package com.forum.controller.forum;

import com.common.entity.ApiResult;
import com.forum.param.ForumUserPasswordParam;
import com.forum.param.ForumUserUpdateInfoParam;
import com.forum.param.LoginParam;
import com.forum.pojo.forum.ForumUser;
import com.forum.service.forum.ForumUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author Tsing
 */

@RestController
@RequestMapping("forum_users")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
//@CrossOrigin(origins = "*")
public class ForumUserController {
    private final ForumUserService forumUserService;

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/register")
    public ApiResult<String> forumUserRegister(@RequestBody LoginParam param){
        return forumUserService.register(param);
    }

    @PostMapping("/login")
    public ApiResult<?> forumUserLogin(@RequestBody LoginParam param){
        return forumUserService.login(param);
    }

    @PostMapping("/info/edit")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<String> editUserInfo(@RequestBody ForumUserUpdateInfoParam updateInfoParam){
        return forumUserService.editUserInfo(updateInfoParam);
    }

    @PostMapping("/password/update")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<String> updateUserPassword(@RequestBody ForumUserPasswordParam passwordParam){
        return forumUserService.updateUserPassword(passwordParam);
    }

    @GetMapping("/info")
    public ApiResult<ForumUser> getCurrentUser(){
        return forumUserService.getCurrentUser();
    }
}
